package servlets.exemplo01;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/hello")
public class MeuServlet extends HttpServlet {
	private static final long serialVersionUID = 1866149206671555635L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/html");
		String param = request.getParameter("nome");
		HttpSession sessao = request.getSession();
		String historico = "";
		if (param!=null) {
			Object obj = sessao.getAttribute("historico");
			sessao.removeAttribute("historico");
			if (obj != null) {
				historico = (String) obj;
			}
			historico = param + " - " + historico;
			sessao.setAttribute("historico", historico);
		}
		//String nav = request.getHeader("User-Agent"); 
		try {
			PrintWriter out = response.getWriter();
			out.println("<html><body>");
			out.println("<h2>Olá "+historico+"!!</h2>");
			out.println("</body></html>");
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		doGet(request, response);
	}
}
